package icraf.carbon.core;

import icraf.carbon.gui.MenuFactory.MenuProperty;

import java.util.ArrayList;
import java.util.Collection;

import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

public class ZoneList {
	//implements ComboBoxModel {

	private ArrayList<Zone> zoneArray = new ArrayList<Zone>();
	private ArrayList<ListDataListener> listDataListener = new ArrayList<ListDataListener>();
	private Project project;
//	private Zone selectedZone;
	private String defaulLabel = MenuProperty.PROJECT_ZONE.getLabel();

	public ZoneList(Project project) {
		super();
		this.project = project;
	}

	protected boolean add(Zone zone) {
		if (zoneArray.contains(zone))
			return false;
		else {
			boolean b = zoneArray.add(zone);
			for (ListDataListener l : listDataListener) {
				l.intervalAdded(new ListDataEvent(this,
						ListDataEvent.INTERVAL_ADDED, getSize() - 1,
						getSize() - 1));
			}
			return b;
		}
	}

	public boolean addNew() {
		Zone z = new Zone();
		z.setLabel(defaulLabel + (zoneArray.size()+1));
		z.setProject(project);
		add(z);
		return true;
	}

	public Zone get(int index) {
		return zoneArray.get(index);
	}

	public Collection<Zone> getZoneList() {
		return zoneArray;
	}

	public Zone remove(int index) {
		if (getSize() <= 1)
			return null;
		Zone gp = zoneArray.remove(index);
		fireZoneRemoved(index, index);
		return gp;

	}

	 public Zone remove(Zone gp) {
		 int index = zoneArray.indexOf(gp);
		 return remove(index);
	 }

	public int indexOf(Zone sp) {
		return zoneArray.indexOf(sp);
	}

	private void fireZoneRemoved(int index0, int index1) {
		for (ListDataListener l : listDataListener) {
			l.intervalRemoved(new ListDataEvent(this,
					ListDataEvent.INTERVAL_REMOVED, index0, index1));
		}

	}

	public void addListDataListener(ListDataListener l) {
		listDataListener.add(l);

	}

	public Object getElementAt(int index) {
		return get(index);
	}

	public int getSize() {
		return zoneArray.size();
	}

	public void removeListDataListener(ListDataListener l) {
		listDataListener.remove(l);
	}

	public void fireContentChanged(int index0, int index1) {
		for (ListDataListener l : listDataListener) {
			l.contentsChanged(new ListDataEvent(this,
					ListDataEvent.CONTENTS_CHANGED, index0, index1));
		}
	}

//	public void removeAll(int startIdx, int endIdx) {
//		for (int i = startIdx; i <= endIdx; i++)
//			zoneArray.remove(startIdx);
//		fireZoneRemoved(startIdx, endIdx);
//	}

	/**
	 * @return the project
	 */
	public AbacusContainer getProject() {
		return project;
	}

//	public Object getSelectedItem() {
//		return selectedZone;
//	}
//
//	public void setSelectedItem(Object anItem) {
//		selectedZone = (Zone) anItem;
//	}

	public void clear() {
		zoneArray.clear();

	}

	public Zone findZone(String label) {
		for (Zone l : zoneArray) {
			if (l.getLabel().equalsIgnoreCase(label.trim()))
				return l;
		}
		return null;
	}
}
